package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author msjykf
 * 每日交易失败数据获取
 *  {"batch_id":"","startdate":"","enddate":"","requesttype": 1,"dk_system":""}
 *  batch_id：批次号;startdate:开始日期;enddate:结束日期;requesttype:请求类型 1定时任务调度 2动态页面调度
 *
 */

public class OboundFailTrade implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(OboundFailTrade.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def jsonbody = Util.getRequestBody(request,'utf-8'); //获取报文
			if(jsonbody.equals("")) {
				return "{\"msg\": \"未获取到入参报文！\",\"code\": -2}"
			};
			JSONObject obj = JSONObject.fromObject(jsonbody); //解析报文
			def requesttype = Util.filterForObject(obj.get("requesttype"));
			if(requesttype.equals("")) {
				return "{\"msg\": \"requesttype参数不能为空！\",\"code\": -2}"
			};
			def batch_id = "";//批次号
			def startdate = Util.filterForObject(obj.get("startdate"));
			def enddate = Util.filterForObject(obj.get("enddate"));
			def dk_system = Util.filterForObject(obj.get("dk_system"));
			if(dk_system.equals("")) {
				dk_system = "*";
			};
			if(startdate.equals("")||enddate.equals("")) {
				return "{\"msg\": \"请输入查询区间！\",\"code\": -2}"
			};
			log.debug("startdate:"+startdate+"enddate:"+enddate);
			def smap = eliteBean.chaxun_sql("select count(*) as findrow from c_calendar where date_i>="+startdate+" and date_i<="+enddate+" and workflag=1","dataSource");
			if(smap[0].get("findrow")==0) {
				return "{\"msg\": \"请查询区间无交易日！\",\"code\": -2}"
			};
			if(requesttype.equals("1")) {
				batch_id = Util.filterForObject(obj.get("batch_id"));
				if(batch_id.equals("")){
				return "{\"msg\": \"批次号不能为空！\",\"code\": -2}"
				};
			}else if(requesttype.equals("2")) {
				smap = eliteBean.chaxun_sql("select customgetid('OB') as batch_id from dual","dataSource");
				batch_id = smap[0].get("batch_id");
				eliteBean.update("insert into obtask_batch(batch_id,taskid,requeststate,procstate,recordcount,startdate,enddate,dk_system)values('"+batch_id+"','6G7NC4',0,0,0,"+startdate+","+enddate+",'"+dk_system+"')","dataSource");
			};
			PropertiesUtil pt= new PropertiesUtil();
			def ibqurl = pt.readValue("ibqurl"); //获取webservice接口地址
			def appid = pt.readValue("appid"); //获取appId
			def charset = "utf-8"; //字符集
			def timeout = 100000; //请求超时时间
			def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:executeProcedure xmlns:m="http://service.web.server.ua.businessmatrix.com/">
									<appId>"""+appid+"""</appId>
									<name>s-P_FAIL_TRAN</name>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>PI_STARTDATE</name>
										<value>"""+startdate+"""</value>
									</params>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>PI_ENDDATE</name>
										<value>"""+enddate+"""</value>
									</params>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>PI_BATCHNO</name>
										<value>"""+batch_id+"""</value>
									</params>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>PI_DK_SYSTEM</name>
										<value>"""+dk_system+"""</value>
									</params>
									</m:executeProcedure>
								</SOAP-ENV:Body>
							</SOAP-ENV:Envelope>""";
			def returnxml = Util.callWebService(ibqurl,xmlbody,charset,"",timeout);
			eliteBean.update("update obtask_batch set requeststate=1,requestdate=sysdate,requestcontent='"+xmlbody+"' where batch_id='"+batch_id+"'","dataSource");
			return "{\"msg\": \"请求接口成功！\",\"code\": 1}"
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}"
		}
	}
}